package org.example.mutithread.sync;

public class Product {

    private String name;
    private String type;

    private volatile boolean flag = false;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public synchronized void produce(String name, String type) {
        if (flag) {
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        this.setName(name);
        this.setType(type);
        System.out.println("生产了：" + this.toString());
        flag = true;
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        this.notify();
    }

    public synchronized void consume() {

        if (!flag) {
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println("消费了：" + this.toString());
        flag = false;
        this.notify();
    }

    @Override
    public String toString() {
        return "Product{" + "name='" + name + '\'' + ", type='" + type + '\'' + '}';
    }
}
